
(load-file "~/.emacs.d/elisp/xcscope.el")
(require 'xcscope)
	(define-key global-map [(shift f3)]  'cscope-set-initial-directory)
	(define-key global-map [(meta f3)]  'cscope-unset-initial-directory)
	(define-key global-map [(control f7)]  'cscope-find-this-symbol)
	(define-key global-map [(f7)]  'cscope-find-global-definition)
	(define-key global-map [(shift f7)]  'cscope-find-this-file)
        (define-key global-map [(f6)] 'cscope-find-functions-calling-this-function)
        (define-key global-map [(control f6)]  'cscope-find-called-functions)

;;	(define-key global-map [(control f7)]
;;	  'cscope-find-global-definition-no-prompting)
	(define-key global-map [(f9)]  'cscope-pop-mark)
	(define-key global-map [(control f8)]  'cscope-next-symbol)
	(define-key global-map [(control f9)] 'cscope-next-file)
	(define-key global-map [(shift f8)] 'cscope-prev-symbol)
	(define-key global-map [(shift f9)] 'cscope-prev-file)
        (define-key global-map [(f8)]  'cscope-display-buffer)


;;;Alt+s : 让speedbar获得焦点
(global-set-key (kbd "M-s") 'speedbar-get-focus)

(setq auto-mode-alist (cons '(".h$" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".C$" . c-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".cc$" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".c$" . c-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".cpp$" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".cxx$" . c++-mode) auto-mode-alist)) 

;; 这里我加了一些自己的目录到load-path之前，
;; 记不清具体是什么原因了，大概是如果不这样的话
;; CEDET还是EDE需要speedbar还有其他一些el文件的时候
;; 会调Emacs自带的同名el文件，版本太低
(setq load-path (append
 '("~/.emacs.d/elisp"
   "~/.emacs.d/elisp/cedet/speedbar"
   "~/.emacs.d/elisp/cedet/common"
   "~/.emacs.d/elisp/cedet/eieio"
   "~/.emacs.d/elisp/cedet/semantic"
   "~/.emacs.d/elisp/cedet/semantic/bovine"
   "~/.emacs.d/elisp/company-mode"
 )
 load-path))

(load-file "~/.emacs.d/elisp/cedet/common/cedet.el")

;; Enabling various SEMANTIC minor modes.  See semantic/INSTALL for more ideas.
;; Select one of the following
;(semantic-load-enable-code-helpers)
;(semantic-load-enable-guady-code-helpers)
(semantic-load-enable-excessive-code-helpers)

(require 'semantic-ia)

; add the gcc include path
(require 'semantic-gcc)

;(semantic-add-system-include "c:/cygwin/usr/include/" 'c-mode)
(semantic-add-system-include "/usr/include" 'c-mode)
(semantic-add-system-include "/usr/include" 'c++-mode)
(semantic-add-system-include "/opt/qnx641/target/qnx6/usr/include" 'c-mode)
(semantic-add-system-include "/opt/qnx641/target/qnx6/usr/include" 'c++-mode)
(semantic-add-system-include "~/work/chaos_demo/chaos/platform/PAL" 'c++-mode)
(semantic-add-system-include "~/work/chaos_demo/chaos/platform/drivers" 'c++-mode)

;; More information about definitions for preprocessor you can find in documentation for the semantic-lex-c-preprocessor-symbol-map variable. List of preprocessor symbols defined for some file with source code you can obtain with the semantic-lex-spp-describe command, and use its result to customize :spp-table parameter or semantic-lex-c-preprocessor-symbol-map variable.

;; Many libraries list all defined macros in one of include files, so you can use these definitions. To do this you need to list these files in the semantic-lex-c-preprocessor-symbol-file variables, and when Cedet will perform analysis, this values will taken into account. By default, this variable has only one value — file with definitions for C++ standard library. As example, I want to show Cedet's configuration for work with Qt4:

;; useful link: http://xtalk.msk.su/~ott/en/writings/emacs-devenv/EmacsCedet.html

(setq qnx-base-dir "/opt/qnx641/target/qnx6/usr/include")
(semantic-add-system-include qnx-base-dir 'c-mode)
(semantic-add-system-include qnx-base-dir 'c++-mode)
(add-to-list 'auto-mode-alist (cons qnx-base-dir 'c-mode))
(add-to-list 'auto-mode-alist (cons qnx-base-dir 'c++-mode))
(add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qnx-base-dir "/errno.h"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qnx-base-dir "/sys/iofunc.h"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qnx-base-dir "/sys/neutrino.h"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qnx-base-dir "/sys/syspage.h"))
(add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qnx-base-dir "/sys/resmgr.h"))


;; if you want to enable support for cscope
;; (require 'semanticdb-cscope)
;; (semanticdb-enable-cscope-databases 'c-mode)
;; (semanticdb-enable-cscope-databases 'c++-mode)

;; if you want to enable support for gnu global
;; (require 'semanticdb-global)
;; (semanticdb-enable-gnu-global-databases 'c-mode)
;; (semanticdb-enable-gnu-global-databases 'c++-mode)

(defun my-cedet-hook ()
  (local-set-key "\C-c?" 'semantic-ia-complete-symbol-menu)
  (local-set-key "\C-c>" 'semantic-complete-analyze-inline)
  (local-set-key "\C-cp" 'semantic-analyze-proto-impl-toggle)
  (local-set-key [(control return)] 'semantic-ia-complete-symbol-menu)
  (local-set-key [(control tab)] 'semantic-complete-analyze-inline)
  )

(add-hook 'c-mode-common-hook 'my-cedet-hook)
(add-hook 'c++-mode-common-hook 'my-cedet-hook)

(defun my-c-mode-cedet-hook ()
 (local-set-key "." 'semantic-complete-self-insert)
 (local-set-key ">" 'semantic-complete-self-insert)
)

(add-hook 'c-mode-common-hook 'my-c-mode-cedet-hook)


;;;;配置Semantic分析文件的目录
(setq semanticdb-default-save-directory (expand-file-name "~/.emacs.tmp/semantic.cache"))

(eval-after-load "semantic-complete"
'(setq semantic-complete-inline-analyzer-displayor-class
semantic-displayor-ghost))


;;;;配置Sementic的检索范围


;; (setq semanticdb-project-roots 
;; 	  (list "~/work"))

;; (global-srecode-minor-mode 1)

;; (global-semanticdb-minor-mode 1)
;; (semantic-reset-system-include 'c-mode)
;; (setq semantic-c-dependency-system-include-path 
;; 	  (list "/usr/lib" "/lib")
;;           )


;; 这个用来控制Emacs闲置多久以后(单位为秒)，CEDET开始调用一些函数
;; 比如说idle-summary，就是下面显示的函数原型(显示格式可以进行配置)
;; 但是比较复杂的数据结构不会进行更新
;; Time in seconds of idle before scheduling events
(setq semantic-idle-scheduler-idle-time 1)
;; 这个多了个work，是用来更新复杂的数据结构的
;; Time in seconds of idle before scheduling big work.
(setq semantic-idle-scheduler-work-idle-time 10)

;; Don't reparse really big buffers.
(setq semantic-idle-scheduler-max-buffer-size 100000)

;; 设置semantic自动完成时调用的tag解析函数;
;; 我都弄成uml-prototype了，因为默认的解析函数只给出参数名，不给出参数类型
;; uml-prototype的显示像这样：func(int a, int b): int
;; 默认的好像是: func(a,b) 这样，很不方便
(setq semantic-analyze-summary-function 'semantic-format-tag-uml-prototype)
(setq semantic-ia-completion-format-tag-function 'semantic-format-tag-uml-prototype)
(setq semantic-ia-completion-menu-format-tag-function 'semantic-format-tag-uml-prototype)
;; 设置semantic idle summary的tag解析函数
;; 这个控制minibuffer里显示的函数原型的格式
;; (setq semantic-idle-summary-function 'semantic-format-tag-uml-prototype)
(setq semantic-idle-summary-function 'semantic-format-tag-summarize-with-file)

(autoload 'senator-try-expand-semantic "senator")


(add-hook 'c-mode-hook 'hs-minor-mode)
(add-hook 'c++-mode-hook 'hs-minor-mode)

;C&CPP  mode

(defun my-c-mode-auto-pair ()
  (interactive)
  (make-local-variable 'skeleton-pair-alist)
  (setq skeleton-pair-alist  '(
    (?` ?` _ "''")
    (?\( _ ")")
    (?\[ _ "]")
    (?{ > \n > _ \n ?} >)))
  (setq skeleton-pair t)
  (local-set-key (kbd "(") 'skeleton-pair-insert-maybe)
  (local-set-key (kbd "{") 'skeleton-pair-insert-maybe)
  (local-set-key (kbd "`") 'skeleton-pair-insert-maybe)
  (local-set-key (kbd "[") 'skeleton-pair-insert-maybe))

(add-hook 'c-mode-hook 'my-c-mode-auto-pair)
(add-hook 'c++-mode-hook 'my-c-mode-auto-pair)

;;;;我的C/C++语言编辑策略

(defun my-c-mode-common-hook()
(c-set-style "linux")
(setq         c-basic-offset 4
              tab-width 4
              indent-tabs-mode t)
(setq tab-width 4 indent-tabs-mode nil)
   ;;hungry-delete and auto-newline
(c-toggle-auto-hungry-state 1)
;;default set all buffer read only 
;(setq buffer-read-only t)
  ;;按键定义
(define-key c-mode-base-map [(return)] 'newline-and-indent)
 ; (define-key c-mode-base-map [(f7)] 'compile)
 ; (define-key c-mode-base-map [(f8)] 'ff-get-other-file)
;;  (define-key c-mode-base-map [(meta \`)] 'c-indent-command)
;;  (define-key c-mode-base-map [(tab)] 'hippie-expand)
;;  (define-key c-mode-base-map [(tab)] 'my-indent-or-complete)
;;  (define-key c-mode-base-map [(control return)] 'semantic-ia-complete-symbol-menu)
;;  (define-key c-mode-base-map [(control tab)] 'semantic-complete-analyze-inline)
  (setq c-macro-shrink-window-flag t)
  ;;(setq c-macro-preprocessor "cpp")
  ;;(setq c-macro-cppflags " ")
  ;;(setq c-macro-prompt-flag t)
  ;;(setq abbrev-mode t)
  ;;(setq tab-width 4 indent-tabs-mode nil)
)

(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)



(defun my-c++-mode-hook()
 (setq tab-width 4 indent-tabs-mode nil)
 (c-set-style "stroustrup")
; (define-key c++-mode-map [f3] 'replace-regexp)
(c-toggle-auto-hungry-state 1)
;;default set all buffer read only 
;(setq buffer-read-only t)
  ;;按键定义
(define-key c++-mode-base-map [(return)] 'newline-and-indent)
 ; (define-key c-mode-base-map [(f7)] 'compile)
 ; (define-key c-mode-base-map [(f8)] 'ff-get-other-file)
;;  (define-key c-mode-base-map [(meta \`)] 'c-indent-command)
;;  (define-key c-mode-base-map [(tab)] 'hippie-expand)
;;  (define-key c-mode-base-map [(tab)] 'my-indent-or-complete)
;(local-set-key (kbd "M-/") 'semantic-complete-analyze-inline) 
)
(add-hook 'c++-mode-common-hook 'my-c++-mode-hook)



;;;     * C-c d ? will look up documentation for the symbol under the point.
;;;     * C-c d r will rescan your Doxygen tags file.
;;;     * C-c d f will insert a Doxygen comment for the next function.
;;;     * C-c d i will insert a Doxygen comment for the current file.
;;;     * C-c d ; will insert a Doxygen comment for a member variable on the current line (like M-;).
;;;     * C-c d m will insert a blank multi-line Doxygen comment.
;;;     * C-c d s will insert a blank single-line Doxygen comment.
;;;     * C-c d @ will insert grouping comments around the current region.
(add-hook 'c-mode-common-hook 'doxymacs-mode)
(add-hook 'c++-mode-common-hook 'doxymacs-mode)

;(defun my-c-mode-complete (character)
;  (insert character)
;  (condition-case nil
;      (progn (semantic-complete-analyze-inline)
;	     (when (semantic-completion-inline-active-p)
;	       (semantic-complete-inline-force-display)))
;    (error nil)))

;;(defun my-c-mode-common-hook1 ()
;;  (local-set-key (kbd ".") '(lambda () (interactive) (my-c-mode-complete ?.)))
;;  (local-set-key (kbd ">") '(lambda () (interactive) (my-c-mode-complete ?>))))

;;(add-hook 'c-mode-common-hook
;;       '(lambda ()
;;          (my-c-mode-common-hook1)))



;;;;C/C++语言启动时自动加载semantic对/usr/include的索引数据库
;;(setq semanticdb-search-system-databases t)
;;  (add-hook 'c-mode-common-hook
;;           (lambda ()
;;             (setq semanticdb-project-system-databases
;;                   (list (semanticdb-create-database
;;                            semanticdb-new-database-class
;;                            "/usr/include")))))


;(add-to-list 'load-path "~/.emacs.d/elisp/pde/lisp")
;(load "pde-load")



(add-hook 'cperl-mode-hook 'hs-minor-mode)
(add-hook 'vhdl-mode-hook 'hs-minor-mode)

;;vhdl

(load "vhdl-goto-def")

;(defun my-prog-read-only-hook()
;  (setq buffer-read-only t)
;  )

;(add-hook 'c-mode-common-hook 'my-prog-read-only-hook)
;(add-hook 'cperl-mode-hook 'my-prog-read-only-hook)
;(add-hook 'vhdl-mode-hook 'my-prog-read-only-hook)

(defvar xref-key-binding 'none)
(setq exec-path (cons "/home/ruiwang/.emacs.d/elisp/xref" exec-path))
(setq load-path (cons "/home/ruiwang/.emacs.d/elisp/xref/emacs" load-path))
(load "xrefactory")
